<?php

namespace app\admin\controller;

use app\model\RegisterField;
use app\model\Address;
use app\model\Area;
use app\model\Order;
use app\model\Goods;
use app\model\GoodsToCategory;

class UpgradeController extends Base
{

	public function index()
	{
		$files = dirname(dirname(dirname(dirname(__DIR__)))) . '/upgrade.php';
		include $files;
		RegisterField::datainitial('member');
		RegisterField::datainitial('agent');
		RegisterField::datainitial('technical');
		RegisterField::datainitial('store');

		$Addressdata = Address::select()->toArray();

		foreach ($Addressdata as $avo) {
			if (empty($avo['city_name'])) {
				Address::where('id', $avo['id'])->update([
					'province_name' => Area::get_area_name($avo['province_id']),
					'city_name' => Area::get_area_name($avo['city_id']),
					'district_name' => Area::get_area_name($avo['district_id'])
				]);
			}
		}

		$Orderdata = Order::select()->toArray();

		foreach ($Orderdata as $ovo) {
			if (empty($ovo['shipping_city_name'])) {
				Order::where('id', $ovo['id'])->update([
					'shipping_province_name' => Area::get_area_name($ovo['shipping_province_id']),
					'shipping_city_name' => Area::get_area_name($ovo['shipping_city_id']),
					'shipping_district_name' => Area::get_area_name($ovo['shipping_district_id'])
				]);
			}
		}

		$Goodsdata = Goods::where('cat_id', 0)->select()->toArray();
		foreach ($Goodsdata as $gvo) {
			$gtc = GoodsToCategory::where('goods_id', $gvo['id'])->find();
			if ($gtc) {
				Goods::where('id', $gvo['id'])->update(['cat_id' => $gtc->category_id]);
			}
		}

		return $this->json(['data' => '更新功成!']);
	}
}
